package com.hk.service.impl;

import com.hk.dao.DisplayDao;
import com.hk.model.Display;
import com.hk.model.ProductMenu;
import com.hk.service.IDisplayService;
import com.hk.service.IProductDisplayService;

import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/19 9:42
 * State：产品展示业务逻辑层
 */
public class DisplayServiceImpl implements IDisplayService {
    @Override
    public List<Display> findAllDisplay() {
//        获取所有产品展示
        DisplayDao displayDao = new DisplayDao();
        List<Display> displays = displayDao.findAllDisplay();
//        获取产品父子级菜单
        IProductDisplayService iProductDisplayService = new ProductDisplayServiceImpl();
        List<ProductMenu> productMenus = iProductDisplayService.findByParentId(0);
        if (displays == null || displays.isEmpty()) {
            System.out.println("产品展示没有数据");
            return null;
        }
        //            遍历父菜单
        for (ProductMenu productMenu : productMenus) {
            for (Display display : displays) {
//                    判断产品属于哪个一级菜单
                if (display.getMenuId() == productMenu.getMenuId()) {
//                        封装一级菜单
                    ProductMenu productMenu1 = new ProductMenu(
                            productMenu.getMenuId(),
                            productMenu.getMenuName(),
                            productMenu.getMenuUrl(),
                            productMenu.getParentId()
                    );
                    display.setProductMenu1(productMenu1);
//            遍历子菜单
                    for (ProductMenu menu : productMenu.getProductMenus()) {
//                            判断产品属于哪个子菜单
                        if (display.gettMenuId() == menu.getMenuId()) {
                            display.setProductMenu(menu);
                        }
                    }
                }
            }
        }
        return displays;

    }
}
